<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-operator">
      <a-form layout="inline" >
        <a-row  :gutter="24">
          <a-col :span="24">
              账号：
              <a-input placeholder="请输入用户账号" v-model="queryParam.loginCode" style="width: 150px;" ></a-input>
              姓名：
              <a-input placeholder="请输入用户姓名" v-model="queryParam.loginName" style="width: 150px;"></a-input>
              状态：
              <j-dict-select-tag  placeholder="请选择授权状态" style="width: 150px;" v-model="queryParam.equAuth" dict-code="equAuth"/>
                &nbsp;&nbsp; &nbsp; &nbsp;
            <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              授权开关
              <a-switch  :checked="this.checked" @change="onFlagChange"></a-switch>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" @click="addEquAuth" icon="check" style="margin-left: 8px">授权</a-button>
      <a-button type="primary" @click="cancleEquAuth" icon="close" style="margin-left: 8px">取消授权</a-button>
      <a-button type="primary" @click="addWhiteList" icon="plus-square" style="margin-left: 8px">添加白名单</a-button>
      <a-button type="primary" @click="cancelWhiteList" icon="minus-square" style="margin-left: 8px">移出白名单</a-button>
      <a-button type="primary" @click="turnDown" icon="minus-square" style="margin-left: 8px">授权驳回</a-button>
      <a-dropdown v-if="selectedRowKeys.length == 1">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 单个操作 <a-icon type="down" /></a-button>
      </a-dropdown>
      <a-dropdown v-if="selectedRowKeys.length >1">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>批量删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <equ-main-modal ref="modalForm" @ok="modalFormOk"></equ-main-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import EquMainModal from './modules/EquMainModal'
  import { getEquMainType,closeOrOpen ,  cancelOrAuthorize,  addOrCancelWhiteList,turnDownGo} from '@/api/app/equMainList'
  import { getAction} from '@/api/manage'
  export default {
    name: 'EquMainList',
    mixins: [JeecgListMixin, mixinDevice],
    components: {
      EquMainModal
    },
    data() {
      return {
        //遮罩层
        loading:false,
        //授权开关状态
        switchFlag: undefined,
        // 查询条件
        queryParam: {
          loginName: '',
          loginCode: '',
          equAuth: '',
        },
        checked: true,
        description: 'MIP_EQU_MAIN管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: "center",
            customRender: function (t, r, index) {
              return parseInt(index) + 1;
            }
          },
          {
            title: '设备编码',
            align: "center",
            dataIndex: 'equSn'
          },
          {
            title: '设备厂商',
            align: "center",
            dataIndex: 'equBrand'
          },
          {
            title: '设备型号',
            align: "center",
            dataIndex: 'equModel'
          },
          {
            title: '设备语言',
            align: "center",
            dataIndex: 'equLanguage'
          },
          {
            title: '用户账号',
            align: "center",
            dataIndex: 'loginCode'
          },
          {
            title: '用户姓名',
            align: "center",
            dataIndex: 'loginName'
          },
          {
            title: 'APP名称',
            align: "center",
            dataIndex: 'appName'
          },
          {
            title: '授权状态',
            align: "center",
            dataIndex: 'equAuth_dictText'
          },
          {
            title: '白名单',
            align: "center",
            dataIndex: 'whiteList_dictText'
          },
          {
            title: '申请时间',
            align: "center",
            dataIndex: 'createTime'
          },
        ],
        url: {
          list: "/equMain/list",
          delete: "/equMain/delete",
          deleteBatch: "/equMain/deleteBatch",
          //授权/取消授权
          cancelOrAuthorize: "/equMain/authorize",
          //添加/移出白名单
          addOrRemoveWhite: "/equMain/addWhiteList",
          //驳回授权
          turnDown: '/equMain/turnDown/',
        },
        dictOptions: {},
      }
    },
    created() {
      this.getEquType();
    },
    computed: {
      importExcelUrl: function () {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      //重新查询
      loadDataAgain(arg) {
        if(!this.url.list){
          this.$message.error("请设置url.list属性!")
          return
        }
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
        }
        var params = this.getQueryParams();//查询条件
        this.loading = true;
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            this.dataSource = res.result.records||res.result;
            if(res.result.total)
            {
              this.ipagination.total = res.result.total;
            }else{
              this.ipagination.total = 0;
            }
            //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          }
          if(res.code===510){
            this.$message.warning(res.message)
          }
          this.loading = false;
        })
      },


      //授权
      addEquAuth: function () {
        var num = [];
        var ids = [];
        if (this.selectedRowKeys.length < 1) {
          this.$message.warning("请至少选择一条记录授权");
        } else {
          ids = this.selectedRowKeys;
          var that = this;
          let obj = {
            ids: ids,
            equAuth: "3"
          }
          this.$confirm({
            title: "确认授权",
            content: "是否授权选中数据?",
            onOk: function () {
              that.loading = true;
              cancelOrAuthorize(that.url.cancelOrAuthorize, obj).then((res) => {
                if (res.success) {
                  that.$message.success("授权成功");
                } else {
                  that.$message.warning("授权失败");
                }
                //增加刷新和清空多选框
                that.selectedRowKeys=[];
                that.loadDataAgain();
              });
              that.loading = false;

            }
          });
        }
      },


      //取消授权
      cancleEquAuth: function () {
        var num = [];
        var ids = [];
        if (this.selectedRowKeys.length < 1) {
          this.$message.warning("请至少选择一条记录取消授权");
        } else {
          ids = this.selectedRowKeys;
          var that = this;
          let obj = {
            ids: ids,
            equAuth: "2"
          }
          this.$confirm({
            title: "确认取消授权",
            content: "是否取消授权选中数据?",
            onOk: function () {
              that.loading = true;
              cancelOrAuthorize(that.url.cancelOrAuthorize, obj).then((res) => {
                if (res.success) {
                  that.$message.success("取消授权成功");
                } else {
                  that.$message.warning("取消授权失败");
                }
                //增加刷新和清空多选框
                that.selectedRowKeys=[];
                that.loadDataAgain();
              }).finally(() => {
                that.loading = false;
              });
            }
          });
        }
      },

      //添加白名单
      addWhiteList() {
        var ids = [];
        if (this.selectedRowKeys.length < 1) {
          this.$message.warning("请至少选择一条记录添加白名单");
        } else {
          ids = this.selectedRowKeys;
          var that = this;
          let obj = {
            ids: ids,
            whiteList: "Y"
          }
          this.$confirm({
            title: "确认加入白名单",
            content: "是否取消选中数据?",
            onOk: function () {
              that.loading = true;
              addOrCancelWhiteList(that.url.addOrRemoveWhite, obj).then((res) => {
                if (res.success) {
                  that.$message.success("加入成功");
                } else {
                  that.$message.warning("加入失败");
                }
                //增加刷新和清空多选框
                that.selectedRowKeys=[];
                that.loadDataAgain();
              }).finally(() => {
                that.loading = false;
              });
            }
          });
        }
      },

      //取消白名单
      cancelWhiteList() {
        var ids = [];
        if (this.selectedRowKeys.length < 1) {
          this.$message.warning("请至少选择一条记录移出白名单");
        } else {
          ids = this.selectedRowKeys;
          var that = this;
          let obj = {
            ids: ids,
            whiteList: "N"
          }
          this.$confirm({
            title: "确认移出白名单",
            content: "是否取消选中数据?",
            onOk: function () {
              that.loading = true;
              addOrCancelWhiteList(that.url.addOrRemoveWhite, obj).then((res) => {
                if (res.success) {
                  that.$message.success("移出成功");
                } else {
                  that.$message.warning("移出失败");
                }
                //增加刷新和清空多选框
                that.selectedRowKeys=[];
                that.loadDataAgain();
              }).finally(() => {
                that.loading = false;
              });
            }
          });
        }
      },

      //授权驳回
      turnDown: function () {
        var ids = [];
        if (this.selectedRowKeys.length < 1) {
          this.$message.warning("请至少选择一条记录驳回");
        } else {
          ids = this.selectedRowKeys;
          var that = this;
          this.$confirm({
            title: "确认驳回",
            content: "是否取消选中数据?",
            onOk: function () {
              that.loading = true;
              turnDownGo(that.url.turnDown, ids).then((res) => {
                if (res.success) {
                  that.$message.success("驳回成功");
                } else {
                  that.$message.warning("驳回失败");
                }
                //增加刷新和清空多选框
                that.selectedRowKeys=[];
                that.loadDataAgain();
              }).finally(() => {
                that.loading = false;
              });
            }
          });
        }
      },


      //获取授权开关的状态
      getEquType() {
        getEquMainType().then(res => {
          this.switchFlag = res.result;
          if (this.switchFlag == "true") {
            this.checked = true;
          } else if (this.switchFlag == "false") {
            this.checked = false;
          }
        });
      },

      //点击触发事件
      onFlagChange(checked) {
        this.checked = checked;
        closeOrOpen(this.checked).then(res => {
          if (res.success()) {
            this.$message.success("成功");
          } else {
            this.$message.success("失败");
          }
        });
      },
    },

}
</script>
<style scoped>
  @import '~@assets/less/common.less';
  .table-operator .ant-btn {
    margin: 3px 8px 4px 0;
  }
</style>